package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.enums.SyncStatusEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.ICopAttachementService;
import com.ibeeking.found.cop.b.rest.service.IFansDefaultWelcomeService;
import com.ibeeking.found.cop.service.common.dos.FansWelcomeDO;
import com.ibeeking.found.cop.service.common.param.CopAttachementParam;
import com.ibeeking.found.cop.service.common.param.FansDefaultWelcomeParam;
import com.ibeeking.found.cop.service.common.param.UniAttachParam;
import com.ibeeking.found.cop.service.common.query.CopAttachementQuery;
import com.ibeeking.found.cop.service.common.query.FansDefaultWelcomeQuery;
import com.ibeeking.found.cop.service.common.vo.CopAttachementVO;
import com.ibeeking.found.cop.service.common.vo.FansDefaultWelcomeVO;
import com.ibeeking.found.cop.service.common.vo.UniAttachVO;
import com.ibeeking.found.cop.service.mapper.FansWelcomeMapper;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName FansDefaultWelcomeServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class FansDefaultWelcomeServiceImpl extends ServiceImpl<FansWelcomeMapper, FansWelcomeDO> implements IFansDefaultWelcomeService {

    @Resource
    private FansWelcomeMapper fansWelcomeMapper;

    @Resource
    private ICopAttachementService copAttachementServiceImpl;

    @Override
    public List<FansDefaultWelcomeVO> list(FansDefaultWelcomeQuery query) {
        List<FansWelcomeDO> entityDOS = fansWelcomeMapper.selectList(new LambdaQueryWrapper<FansWelcomeDO>()
                .eq(query.getId() != null, FansWelcomeDO::getId, query.getId())
                .eq(FansWelcomeDO::getType, 0)
                .like(StringUtils.isNotBlank(query.getContent()), FansWelcomeDO::getContent, query.getContent())
                .eq(query.getSeleted() != null, FansWelcomeDO::getSelected, query.getSeleted())
                .eq(query.getStatus() != null, FansWelcomeDO::getStatus, query.getStatus())
                .orderByDesc(FansWelcomeDO::getCreateTime)
        );
        List<FansDefaultWelcomeVO> entityVOS =JsonUtils.jsonToList(entityDOS, FansDefaultWelcomeVO.class);
        if(Collections3.isNotEmpty(entityVOS)) {
            List<Long> ids = entityVOS.stream().map(FansDefaultWelcomeVO::getId).collect(Collectors.toList());
            CopAttachementQuery attachementQuery = new CopAttachementQuery();
            attachementQuery.setMessageIds(ids);
            List<CopAttachementVO> copAttachementVOS = copAttachementServiceImpl.list(attachementQuery);
            Map<Long, CopAttachementVO> copAttachementMap = Collections3.isNotEmpty(copAttachementVOS)?copAttachementVOS.stream().collect(Collectors.toMap(CopAttachementVO::getMessageId, x->x, (o, n) -> n)):null;

            entityVOS.forEach(x -> {
                x.setStatusDesc(PublishStatusEnum.getName(x.getStatus()));
                x.setSyncStatusDesc(SyncStatusEnum.getName(x.getSyncStatus()));
                CopAttachementVO copAttachementVO = copAttachementMap!=null && copAttachementMap.size()>0?copAttachementMap.get(x.getId()):null;
                if(copAttachementVO!=null){
                    List<UniAttachVO> uniAttachVOS = copAttachementServiceImpl.transerCopAttachementToUniAttachement(copAttachementVO);
                    x.setAttachements(uniAttachVOS);
                }
            });
        }
        return entityVOS;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(FansDefaultWelcomeParam param) {
        FansWelcomeDO entityDO = BeanUtil.convertBean(param, FansWelcomeDO.class);
        Long id = GenerateId.generateId();
        entityDO.setId(id);
        entityDO.setType(0);
        entityDO.setSyncStatus(SyncStatusEnum.SYNC_NO.getValue());
        entityDO.setStatus(PublishStatusEnum.DISABLE.getValue());
        List<UniAttachParam> uniAttachParams = param.getAttachements();
        if(Collections3.isNotEmpty(uniAttachParams)){
            CopAttachementParam copAttachementParam = copAttachementServiceImpl.transerUniAttachementToCopAttachement(uniAttachParams);
            if(copAttachementParam!=null){
                copAttachementParam.setMessageId(id);
                copAttachementServiceImpl.add(copAttachementParam);
            }
        }
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        return fansWelcomeMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(FansDefaultWelcomeParam param) {
        FansWelcomeDO entityDO = BeanUtil.convertBean(param, FansWelcomeDO.class);
        entityDO.setSyncStatus(SyncStatusEnum.SYNC_NO.getValue());
        entityDO.setType(1);
        List<UniAttachParam> uniAttachParams = param.getAttachements();
        if(Collections3.isNotEmpty(uniAttachParams)){
            CopAttachementParam copAttachementParam = copAttachementServiceImpl.transerUniAttachementToCopAttachement(uniAttachParams);
            if(copAttachementParam!=null){
                copAttachementParam.setMessageId(entityDO.getId());
                copAttachementServiceImpl.modify(copAttachementParam);
            }
        }
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return fansWelcomeMapper.updateById(entityDO) > 0;
    }

    @Override
    public FansDefaultWelcomeVO view(Long id) {
        FansWelcomeDO entityDO = fansWelcomeMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        FansDefaultWelcomeVO entityVO = BeanUtil.convertBean(entityDO, FansDefaultWelcomeVO.class);
        if(entityVO != null) {
            entityVO.setStatusDesc(PublishStatusEnum.getName(entityDO.getStatus()));
            entityVO.setSyncStatusDesc(SyncStatusEnum.getName(entityDO.getSyncStatus()));
            CopAttachementVO copAttachementVO = copAttachementServiceImpl.queryByMessageId(id);
            if(null != copAttachementVO) {
                List<UniAttachVO> uniAttachVOS = copAttachementServiceImpl.transerCopAttachementToUniAttachement(copAttachementVO);
                entityVO.setAttachements(uniAttachVOS);
            }
        }
        return entityVO;
    }

    @Override
    public FansDefaultWelcomeVO querySelected() {
        FansDefaultWelcomeQuery query = new FansDefaultWelcomeQuery();
        query.setSeleted(1);
        List<FansDefaultWelcomeVO> fansDefaultWelcomeVOS = this.getBean().list(query);
        if (Collections3.isNotEmpty(fansDefaultWelcomeVOS)){
            return fansDefaultWelcomeVOS.get(0);
        }else{
            return null;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeStatus(Long id, Integer status) {
        FansWelcomeDO entityDO = fansWelcomeMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        entityDO.setStatus(status);
        entityDO.setType(0);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return fansWelcomeMapper.updateById(entityDO) > 0;
    }

    @Override
    public Boolean updateSelected(Long id){
        List<FansWelcomeDO> fansWelcomeDOS = new ArrayList<>();
        FansWelcomeDO oldSelectedfansWelcomeDO = fansWelcomeMapper.selectOne(new LambdaQueryWrapper<FansWelcomeDO>()
                .eq(FansWelcomeDO::getType,0).eq(FansWelcomeDO::getSelected,1));
        if(oldSelectedfansWelcomeDO!=null){
            oldSelectedfansWelcomeDO.setSelected(0);
            oldSelectedfansWelcomeDO.setType(0);
            fansWelcomeDOS.add(oldSelectedfansWelcomeDO);
        }
        FansWelcomeDO newSelectedfansWelcomeDO = fansWelcomeMapper.selectById(id);
        if(newSelectedfansWelcomeDO!=null){
            newSelectedfansWelcomeDO.setSelected(1);
            newSelectedfansWelcomeDO.setType(0);
            fansWelcomeDOS.add(newSelectedfansWelcomeDO);
        }
        if(Collections3.isNotEmpty(fansWelcomeDOS)){
            return super.updateBatchById(fansWelcomeDOS);
        }else{
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        FansWelcomeDO entityDO = fansWelcomeMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        copAttachementServiceImpl.delByMessageId(id);
        return fansWelcomeMapper.deleteById(id) > 0;
    }

    private IFansDefaultWelcomeService getBean(){
        return SpringBeanUtil.getBean(FansDefaultWelcomeServiceImpl.class);
    }
}
